package net.diaowen.dwsurvey.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import net.diaowen.common.base.entity.Group;
import net.diaowen.dwsurvey.entity.SurveyDirectory;
import net.diaowen.dwsurvey.entity.SurveyGroup;
import net.diaowen.dwsurvey.service.GroupManager;
import net.diaowen.dwsurvey.service.SurveyDirectoryManager;
import net.diaowen.dwsurvey.service.SurveyGroupManager;


/**
 * 控制发布的小组
 * @author zhangyuting
 * @date 2019年5月8日
 *
 */
@Controller
@RequestMapping("/design/my-survey-design-group")
public class MySurveyDesignGroupController {


	@Autowired
	private SurveyGroupManager surveyGroupManager;
	
	@Autowired
	private SurveyDirectoryManager surveyDirectoryManager;	
	
	@Autowired
	private GroupManager groupManager;

	
	@RequestMapping("/list.do")
	public ModelAndView list(HttpServletRequest request,String userGroupName,String surveyId) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		try{
			Group entity = new Group();
			entity.setUserGroupName(userGroupName);
			List<Group> groupList = groupManager.findList(entity);
			List<SurveyGroup> surveyGroupList = surveyGroupManager.findListBySurveyId(surveyId);
			modelAndView.addObject("surveyGroupList",surveyGroupList);
			modelAndView.addObject("groupList",groupList);
			modelAndView.addObject("surveyId",surveyId);
			modelAndView.addObject("userGroupName",userGroupName);
		}catch (Exception e) {
			e.printStackTrace();
		}
		modelAndView.setViewName("/diaowen-design/group-list");
		return modelAndView;
	}


	@RequestMapping("/ajaxSave.do")
	public String save(String surveyId,String groupId,HttpServletResponse response) throws Exception {
		surveyGroupManager.adminSave(surveyId, groupId);

		response.getWriter().write("true");
		return null;
	}
	
	@RequestMapping("/ajaxCheck.do")
	public String check(String surveyId,String groupId,HttpServletResponse response) throws Exception {
		//check一下是否勾选了学号检验的checkbox
		SurveyDirectory survey = surveyDirectoryManager.getSurvey(surveyId);
		if (survey.getSurveyDetail().getEffectiveStudentId() == 1) {
			response.getWriter().write("true");
		} else {
			response.getWriter().write("false");
		}
		return null;
	}
}
